home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 July / EnigmA AMIGA RUN 20 (1997)(G.R. Edizioni)(IT)[!][issue 1997-07 & 08][EAR-CD IV].iso / earcd / dev / c / amcsrc2.lha / AMCSources2 / NeuralN / RANDOM.c < prev    next >
C/C++ Source or Header  |  1996-06-06  |  3KB  |  132 lines

  1. /*
  2. *-----------------------------------------------------------------------------
  3. *    file:        random.c
  4. *    desc:        routine for a very-long-cycle random-number sequences
  5. *    by:        patrick ko shu pui
  6. *    date:        6 sep 1991
  7. *    comment:
  8. *
  9. *    this random number generator was proved to generate random sequences
  10. *    between 0 to 1 which if 100 numbers were calculated every second, it
  11. *    would NOT repeat itself for over 220 years.
  12. *
  13. *    reference:
  14. *
  15. *    Wichmann B.A. and I.D. Hill. "Building A Random Number Generator."
  16. *    Byte Magazine. March 1987. pp.127.
  17. *
  18. *    remark:        this C routine is a freeware
  19. *
  20. *    ko053@cucs19.cs.cuhk.hk    Internet 
  21. *    BiG Programming Club (since 1987), Hong Kong, 6:700/132 FidoNet
  22. *    [852] 654-8751
  23. *-----------------------------------------------------------------------------
  24. */
  25. #include    <time.h>
  26. #ifdef AMIGA
  27. #include <float.h>
  28. #include <limits.h>
  29. #define MAXFLOAT FLT_MAX
  30. #define MAXINT   INT_MAX
  31. #else
  32. #include <values.h>
  33. #endif
  34.  
  35.  
  36. #include    "random.h"
  37.  
  38. #define    REAL    double
  39. #define    INT    int
  40.  
  41. /*
  42. *    default seed values
  43. */
  44. static INT    x = 1;
  45. static INT    y = 10000;
  46. static INT    z = 3000;
  47.  
  48. /*
  49. *=============================================================================
  50. *    funct:        rndmize
  51. *    dscpt:        generating random number seeds
  52. *    given:        nothing
  53. *    retrn:        nothing
  54. *=============================================================================
  55. */
  56. INT rndmize()
  57. {
  58.     time_t    timer;
  59.  
  60.     x = time( &timer ) % MAXINT;
  61.     y = (x * x) % MAXINT;
  62.     z = (y * y) % MAXINT;
  63. }
  64.  
  65. /*
  66. *=============================================================================
  67. *    funct:        rnd
  68. *    dscpt:        return a random number of range 0-1
  69. *    given:        nothing
  70. *    retrn:        the random number
  71. *    cmmnt:        you may use prandomize to change the seeds
  72. *=============================================================================
  73. */
  74. REAL rnd()
  75. {
  76.     REAL    temp;
  77.  
  78.     /*
  79.     *    first generator
  80.     */
  81.     x = 171 * (x % 177) - 2 * (x / 177);
  82.     if (x < 0)
  83.         {
  84.         x += 30269;
  85.         }
  86.  
  87.     /*
  88.     *    second generator
  89.     */
  90.     y = 172 * (y % 176) - 35 * (y / 176);
  91.     if (y < 0)
  92.         {
  93.         y += 30307;
  94.         }
  95.  
  96.     /*
  97.     *    third generator
  98.     */
  99.     z = 170 * (z % 178) - 63 * (z / 178);
  100.     if (z < 0)
  101.         {
  102.         z += 30323;
  103.         }
  104.  
  105.     /*
  106.     *    combine to give function value
  107.     */
  108.     temp = x/30269.0 + y/30307.0 + z/30323.0;
  109.  
  110.     return (temp - (INT)temp);
  111. }
  112.  
  113.  
  114. /*
  115. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  116. * * *                E X A M P L E
  117. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  118. */
  119. /*
  120. int main()
  121. {
  122.     int     i;
  123.  
  124.     rndmize();
  125.  
  126.     for (i=0; i<100; i++)
  127.         {
  128.         printf("%f,", rnd() );
  129.         }
  130. }
  131. */
  132.